package com.mlk.rbac.Service.impl;

import javax.annotation.Resource;

import com.mlk.core.component.page.Page;
import com.mlk.core.component.page.Pager;
import com.mlk.core.util.DictUtils;
import com.mlk.core.util.ServerResponse;
import com.mlk.rbac.Service.SysDictService;
import com.mlk.rbac.dto.SysDictDTO;
import com.mlk.rbac.mapper.SysDictMapper;
import com.mlk.rbac.mapper.SysDictTypeMapper;
import com.mlk.rbac.po.SysDict;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统数据字典服务
 * @author trek
 */
@Service
public class SysDictServiceImpl implements SysDictService {

	@Resource
	private SysDictMapper sysDictMapper;

	@Resource
	private SysDictTypeMapper sysDictTypeMapper;
	
	@Override
	@Page
	public Pager<SysDictDTO> dictList(String dictType) {
		return Pager.adapt(this.sysDictMapper.dictList(dictType));
	}
	
	@Override
	@Transactional
	public ServerResponse dictDelete(Integer id) {
		
		if(id == null) {
			return  ServerResponse.fail("删除的字典项不存在");
		}
        Integer dictTypeId = this.sysDictMapper.selectById(id).getDictType();
		int count = this.sysDictMapper.deleteById(id);
		if(count == 0) {
			return ServerResponse.fail("删除字典项失败");
		}
		//刷新缓存的内容
		refreshDictCacheByDictTypeId(dictTypeId);
		return ServerResponse.ok("删除字典项成功");
	}
	
	@Override
    @Transactional
	public ServerResponse dictUpdate(SysDictDTO dict) {
		if(dict ==null) {
			return ServerResponse.fail("更新的字典不存在");
		}
		
		if(dict.getId() == null) {
			return ServerResponse.fail("更新没有传入字典的id");
		}
		
		SysDict sysDict = new SysDict();
		BeanUtils.copyProperties(dict, sysDict);
		
		int count = this.sysDictMapper.updateById(sysDict);
		if(count == 0) {
			return ServerResponse.fail("更新字典失败");
		}
        //刷新缓存内容
        this.refreshDictCacheByDictItemId(dict.getId());
		return ServerResponse.ok("更新字典成功");
	}
	
	@Override
    @Transactional
	public ServerResponse dictAdd(SysDict dict) {
		
		if(dict == null) {
			return ServerResponse.fail("新增的字典不能为空");
		}
		
		if(dict.getDictType() == null) {
			return ServerResponse.fail("没有传入字典的类型");
		}
		
		int count = this.sysDictMapper.selectCount(new EntityWrapper<>(SysDict.builder().dictType(dict.getDictType()).dictKey(dict.getDictKey()).build()));
		
		if(count != 0) {
			return ServerResponse.fail("在该字典组之中存在相同键的字典映射");
		}
		
		count = this.sysDictMapper.insert(dict);

		//刷新缓存内容
        refreshDictCacheByDictTypeId(dict.getDictType());
		
		if(count == 0) {
			return ServerResponse.fail("添加字典失败");
		}
		
		return ServerResponse.ok("添加字典成功");
	}

    /**
     * 根据字典项的id刷新缓存内容
     * @param dictItemId
     */
	private void refreshDictCacheByDictItemId(Integer dictItemId){
        Integer dictTypeId = this.sysDictMapper.selectById(dictItemId).getDictType();
        refreshDictCacheByDictTypeId(dictTypeId);
    }

    /**
     * 根据字典类型id刷新缓存
     * @param dictTypeId
     */
    private void refreshDictCacheByDictTypeId(Integer dictTypeId){
        String typeDesc = this.sysDictTypeMapper.selectById(dictTypeId).getTypeDesc();
        DictUtils.refreshCache(this.sysDictMapper.queryAllDictItem(typeDesc));
    }
	
}
